package com.dexilaonian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dexilaonian.MainActivity;
import com.dexilaonian.R;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.PreferencesUtil;
import com.dexilaonian.utils.WorksSizeCheckUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class Input_NewPwdActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title_top)
    TextView titleTop;
    @Bind(R.id.base_login)
    TextView baseLogin;
    @Bind(R.id.et_set_pwd)
    EditText etSetPwd;

    @Bind(R.id.ed_ensure_pwd)
    EditText edEnsurePwd;
    @Bind(R.id.btn_newpwd)
    Button btnNewpwd;
    @Bind(R.id.cbDisplayPassword)
    CheckBox cbDisplayPassword;
    @Bind(R.id.cbDisplayPassword2)
    CheckBox cbDisplayPassword2;
    private String url_pwd;
    private String sms_phone;
    private String change_url;

    @Override
    protected void initData() {
    }
    @Override
    public int getLayoutID() {
        return R.layout.activity_input__new_pwd;
    }

    @Override
    public void initView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        titleTop.setText("设置密码");
        Intent intent = getIntent();
        String count = intent.getStringExtra("count");
        if (count.equals("0")) {
            url_pwd = Constant.base_url + "/user/registerByPhone";
        } else if (count.equals("1")) {
            url_pwd = Constant.base_url + "/user/RestPasswordByPhone";
        }
        sms_phone = intent.getStringExtra("sms_phone");
        cbDisplayPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    etSetPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

                } else {
                    etSetPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                etSetPwd.setSelection(etSetPwd.length());//将光标移至文字末尾
            }
        });

        cbDisplayPassword2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    edEnsurePwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    edEnsurePwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                edEnsurePwd.setSelection(edEnsurePwd.length());//将光标移至文字末尾
            }
        });
        WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(btnNewpwd);
        textChangeListener.addAllEditText(etSetPwd, edEnsurePwd);
        WorksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if (isHasContent) {
                    btnNewpwd.setBackground(getResources().getDrawable(R.drawable.button));
                    btnNewpwd.setClickable(true);
                } else {
                    btnNewpwd.setBackground(getResources().getDrawable(R.drawable.graybutton));
                    btnNewpwd.setClickable(false);
                }
            }
        });
        btnNewpwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String setPwd = etSetPwd.getText().toString().trim();
                String enSurePwd = edEnsurePwd.getText().toString().trim();
                if (TextUtils.isEmpty(setPwd)) {
                    showToast("请输入新密码");
                    return;
                }
                if (TextUtils.isEmpty(enSurePwd)) {
                    showToast("请确认新密码");
                    return;
                }
                if (setPwd.length() < 6 || setPwd.length() > 20) {
                    showToast("密码长度不正确");
                    return;
                }
                if (setPwd.equals(enSurePwd)) {
                    NewPwd(sms_phone, enSurePwd);
                } else {
                    showToast("两次输入密码不同");
                    return;
                }
            }
        });
    }

    @OnClick({R.id.back, R.id.base_login, R.id.btn_newpwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.base_login:
                startActivity(new Intent(Input_NewPwdActivity.this, LoginActivity.class));
                break;
            case R.id.btn_newpwd:
                break;
        }
    }
    private void NewPwd(String mysmsPhone, String pwd) {
        Log.e("MMM", mysmsPhone + "............" + pwd);
        showProgressBar(true);

        OkHttpUtils.post().url(url_pwd).addParams("phone", mysmsPhone).addParams("password", pwd).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showProgressBar(false);
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                showProgressBar(false);
                Log.e("TTTensurePWD", response);
                try {
                    JSONObject object = new JSONObject(response);
                    int status = Integer.parseInt(object.optString("status"));
                    JSONObject data = object.optJSONObject("data");
                    if (data != null) {
                        String token = data.optString("token");
                        if (!TextUtils.isEmpty(token)) {
                            PreferencesUtil.putString(Input_NewPwdActivity.this, Constant.KEY, token);
                        }
                    }
                    if (status == 2) {
                        showToast("该号码已经注册，可找回密码");
                        return;
                    }
                    if (status == 9) {
                        showToast("密码长度不正确");
                        return;
                    }
                    if (status == 11) {
                        showToast("用户未注册");
                        return;
                    }
                    if (status == 28) {
                        showToast("新密码不能与旧密码一致");
                        return;
                    }
                    if (status == 0) {
                        showToast("注册成功");
                        Intent intent = new Intent(Input_NewPwdActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        finish();
                    } else {
                        showToast("注册失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}

